package Algorithm.LinkedListProblem;

/**
 * @Author cj
 * @Date 2021/11/20 12:02
 */
public class removeByRatio {

    class Node {
        public int value;
        public Node next;

        public Node(int val) {
            this.value = val;
        }
    }

    public static Node removeByRatio1(Node head, int a, int b) {
        if (head == null || a < 1 || a > b) {
            return head;
        }
        int n = 0;
        Node cur = head;
        while (cur != null) {
            n++;
            cur = cur.next;
        }
        int index = (int) Math.ceil(((double) (n * a)) / (double) b);
        // 考虑index=1情况
        if (index == 1) {
            return head.next;
        }
        if (index > 1) {
            // 删除
            cur = head;
            for (int i = 0; i < index - 1; i++) {
                cur = cur.next;
            }
            cur.next = cur.next.next;
        }
        return head;
    }
}
